package
{
	import flash.display.DisplayObject;
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.geom.Point;
	import flash.text.*;

	/** Used to represent any closed polygon on the construction panel */
	public class ClosedPolygon extends Polygon
	{
		public static const HIGHLIGHT_COLOR:uint = 0xFFFF00;
		public static const OUTLINE_COLOR:uint = 0x0000FF;
		public static const USED_COLOR:uint = 0xFF0000;
		// public vars
		public var attachedType:String = "";
		// private objects
		private var usedTField:TextField, resetTField:TextField;
		private var _childPolygon:ChildPolygon;
		// private vars
		private var _highlighted:Boolean, _outlining:Boolean, _used:Boolean=false;
		public function get highlighted():Boolean{return _highlighted;}
		public function set highlight(b:Boolean):void{_highlighted=b; redraw();}
		public function get outlining():Boolean{return _outlining;}
		public function set outline(b:Boolean):void{_outlining=b; redraw();}
		public function get used():Boolean{return _used;}
		//public function set used(b:Boolean):void{_used=b; redraw();}
		public function get childPolygon():ChildPolygon{return _childPolygon;}
		public function set childPolygon(c:ChildPolygon):void{if(c!=null){_used=true}else{_used=false}; _childPolygon=c; redraw();}
		/** Creates a new polygon with inputted points */
		public function ClosedPolygon(drawPoints:Array)
		{
			super(null, 0, 0);
			this.drawPoints = drawPoints;	
			this.points = DrawPoint.toGeomPoints(this.drawPoints);
			//trace("closed polygon", this.points);
			// create a text field that will appear when a selected polygon is used
			usedTField = new TextField();
			usedTField.selectable = false;
			usedTField.mouseEnabled = false;
			usedTField.defaultTextFormat = new TextFormat("arial", 20, 0x440000);
			usedTField.text = "Used";
			usedTField.autoSize = "left";
			// the reset TField can actually be pressed
			resetTField = new TextField();
			resetTField.selectable = false;
			resetTField.mouseEnabled = true;
			resetTField.border = true;
			resetTField.borderColor = 0x440000;
			resetTField.defaultTextFormat = new TextFormat("arial", 20, 0x440000);
			resetTField.text = "Reset";
			resetTField.autoSize = "left";
			resetTField.background = true;
			resetTField.backgroundColor = 0xFF8888;
			redraw();
		}
		
		/** Creates a temporary drawn shape, connecting all points, and tests whether input point is within the shapes bounds */
		/*override public function pointWithinBounds(callingObject:DisplayObject, point:Point, useRayCasting:Boolean=true):Boolean
		{
			this.points = DrawPoint.toGeomPoints(this.drawPoints);
			
			var gpoint:Point = callingObject.localToGlobal(point);
			var sprite:Sprite = new Sprite();
			sprite.graphics.moveTo(points[points.length-1].x, points[points.length-1].y);
			sprite.graphics.beginFill(0xFFFFFF,1);
			for each (var p:Point in points)
			{
				sprite.graphics.lineTo(p.x, p.y);
			}
			sprite.graphics.endFill();
			this.addChild(sprite);
			var hit:Boolean = sprite.hitTestPoint(gpoint.x, gpoint.y, true);
			this.removeChild(sprite);
			//var hit:Boolean = backgroundShape.hitTestPoint(gpoint.x, gpoint.y, true);
			if (hit)
			{
				return true;
			}
			else 
			{
				return false;
			}
		}
		*/
		override public function redraw():void
		{
			//trace("redraw:", "used", this.used, "highlighted", this.highlighted, "outlining", this.outlining)
			this.points = DrawPoint.toGeomPoints(this.drawPoints);
			if (used && outlining )
			{
				this.graphics.clear();
				this.graphics.moveTo(points[points.length-1].x, points[points.length-1].y);
				this.graphics.lineStyle(8, USED_COLOR, .5);
				this.graphics.beginFill(USED_COLOR,.5);
				for each (p in points)
				{
					this.graphics.lineTo(p.x, p.y);				
				}
				this.graphics.endFill();
				if (usedTField.parent != this) this.addChild(usedTField);
				if (resetTField.parent != this) this.addChild(resetTField);
				usedTField.x = Polygon.Centroid(points).x-usedTField.width/2;
				usedTField.y = Polygon.Centroid(points).y-usedTField.height/2;
				resetTField.x = Polygon.Centroid(points).x-usedTField.width/2;
				resetTField.y = Polygon.Centroid(points).y+resetTField.height/2;
				//trace(MathUtilities.area(points), MathUtilities.findBoundingWidth(points), MathUtilities.findBoundingHeight(points),Polygon.Centroid(points), usedTField.x, usedTField.y, resetTField.x, resetTField.y);
				resetTField.addEventListener(MouseEvent.MOUSE_OVER, handleMouseOverReset);
			}
			else if (highlighted)
			{
				if (usedTField.parent == this) this.removeChild(usedTField);
				if (resetTField.parent == this){ this.removeChild(resetTField); resetTField.removeEventListener(MouseEvent.MOUSE_OVER, handleMouseOverReset);}
				// draw something just for show;
				this.graphics.clear();
				this.graphics.moveTo(points[points.length-1].x, points[points.length-1].y);
				this.graphics.lineStyle(8, HIGHLIGHT_COLOR, .5);
				//this.graphics.beginFill(HIGHLIGHT_COLOR,.5);
				for each (var p:Point in points)
				{
					this.graphics.lineTo(p.x, p.y);				
				}
				//this.graphics.endFill();
			} else if (outlining)
			{
				if (usedTField.parent == this) this.removeChild(usedTField);
				if (resetTField.parent == this){ this.removeChild(resetTField); resetTField.removeEventListener(MouseEvent.MOUSE_OVER, handleMouseOverReset);}
				// draw something just for show;
				this.graphics.clear();
				this.graphics.moveTo(points[points.length-1].x, points[points.length-1].y);
				this.graphics.lineStyle(8, OUTLINE_COLOR, .5);
				this.graphics.beginFill(OUTLINE_COLOR,.5);
				for each (p in points)
				{
					this.graphics.lineTo(p.x, p.y);				
				}
				this.graphics.endFill();
			}
			else
			{
				if (usedTField.parent == this) this.removeChild(usedTField);
				if (resetTField.parent == this){ this.removeChild(resetTField); resetTField.removeEventListener(MouseEvent.MOUSE_OVER, handleMouseOverReset);}
				this.graphics.clear();
				
				this.graphics.moveTo(points[points.length-1].x, points[points.length-1].y);
				//this.graphics.lineStyle(8, OUTLINE_COLOR, .5);
				this.graphics.beginFill(OUTLINE_COLOR,.05);
				for each (p in points)
				{
					this.graphics.lineTo(p.x, p.y);				
				}
				this.graphics.endFill();
				
			}
		}
		
		public function handleMouseOverReset (event:MouseEvent):void
		{
			resetTField.removeEventListener(MouseEvent.MOUSE_OVER, handleMouseOverReset);
			resetTField.borderColor = 0x220000;
			resetTField.addEventListener(MouseEvent.MOUSE_OUT, handleMouseOutReset);	
			resetTField.addEventListener(MouseEvent.CLICK, handleClickReset);
		}
		public function handleMouseOutReset (event:MouseEvent):void
		{
			resetTField.removeEventListener(MouseEvent.MOUSE_OUT, handleMouseOutReset);	
			resetTField.removeEventListener(MouseEvent.CLICK, handleClickReset);
			resetTField.borderColor = 0x440000;
			resetTField.addEventListener(MouseEvent.MOUSE_OVER, handleMouseOverReset);
		}
		public function handleClickReset (event:MouseEvent):void
		{
			resetTField.removeEventListener(MouseEvent.MOUSE_OUT, handleMouseOutReset);	
			resetTField.removeEventListener(MouseEvent.CLICK, handleClickReset);
			//resetTField.addEventListener(MouseEvent.MOUSE_OVER, handleMouseOverReset);
			_highlighted = false;
			//trace("pre redraw");
			//used = false;
			childPolygon = null;
			GeometryGameItem(parent.parent).processCommand("child", "complete", parent, new Array(attachedType));
			attachedType = "";			
		}
	}
}